<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:type:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center" prop="typeName" />
      <el-table-column label="属性数量" align="center" prop="attitudeCount"  />
      <el-table-column label="参数数量" align="center" prop="propertyCount" />
      <el-table-column label="设置" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleShowAttitude(scope.row.id)"
          >属性列表</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleShowProperty(scope.row.id)"
          >参数列表</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:property:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:property:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="properTitle" :visible.sync="properForm" width="800px" append-to-body>

      <el-form ref="form" :model="editForm" :rules="rules" label-width="150px">
        <el-form-item label="类型名称" >
          <el-input v-model="editForm.propertyName" placeholder="类型名称" />
        </el-form-item>
        <el-form-item label="商品类型" >
          <el-select v-model="editForm.typeId" placeholder="请选择"  clearable>
            <el-option
              v-for="c in typeLists"
              :key="c.id"
              :label="c.typeName"
              :value="c.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类筛选样式" >
          <el-radio-group v-model="editForm.style">
            <el-radio :label="1" >普通</el-radio>
            <el-radio :label="2">颜色</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="能否进行检索" >
          <el-radio-group v-model="editForm.search">
            <el-radio :label="1" >不需要检索</el-radio>
            <el-radio :label="2">关键词检索</el-radio>
            <el-radio :label="3">检索范围</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类筛选样式" >
          <el-radio-group v-model="editForm.relevance">
            <el-radio :label="true" >是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="能否进行检索" >
          <el-radio-group v-model="editForm.selected">
            <el-radio :label="1" >唯一</el-radio>
            <el-radio :label="2">单选</el-radio>
            <el-radio :label="3">复选</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="能否进行检索" >
          <el-radio-group v-model="editForm.method">
            <el-radio :label="1" >手工录入</el-radio>
            <el-radio :label="2">从下面列表中选择</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="属性值可选列表" >
          <el-input v-model="editForm.propertyList" placeholder="属性值可选列表" />
        </el-form-item>
        <el-form-item label="是否支持手动" >
          <el-radio-group v-model="editForm.supportHand">
            <el-radio :label="true" >是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" >
          <el-input v-model="editForm.propertyRank" placeholder="排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProperForm">确 定</el-button>
        <el-button @click="cancelProperForm">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="属性列表" width="80%" :visible.sync="dialogTableVisible">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handlePropertyAdd"
            v-hasPermi="['produce:property:add']"
          >新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="properties" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            {{scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column label="属性名称" align="center" prop="propertyName" />
        <el-table-column label="商品类型" align="center" prop="typeName" />
        <el-table-column label="属性是否可选" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <p v-show="scope.row.selected===1">
              唯一
            </p>
            <p v-show="scope.row.selected===2">
              单选
            </p>
            <p v-show="scope.row.selected===3">
              复选
            </p>
          </template>
        </el-table-column>
        <el-table-column label="级别" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <p v-show="scope.row.method===1">
              手工录入
            </p>
            <p v-show="scope.row.method===2">
              列表选择
            </p>
          </template>
        </el-table-column>
        <el-table-column label="商品数量" align="center" prop="propertyList"  />
        <el-table-column label="排序" align="center" prop="propertyRank" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handlePropertyEdit(scope.row.id)"
              v-hasPermi="['produce:brand:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handlePropertyDelete(scope.row.id)"
              v-hasPermi="['produce:brand:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="类型名称" >
          <el-input v-model="form.typeName" placeholder="类型名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType,addType, updateType,delType ,getType,allType} from "@/api/produce/type";
import { listProperty,addProperty, updateProperty,delProperty ,getProperty } from "@/api/produce/property";
export default {
  name: "type",
  data() {
    return {
      classifies:[],

      properties:[],

      typeLists:[],
      // 遮罩层
      loading: true,

      dialogTableVisible: false,

      properForm: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      typeList: [],
      // 一级分类
      type: [],
      // 弹出层标题
      title: "",

      properTitle: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      editForm: {},
      // 表单校验
      rules: {
        typeName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {



    handleShowAttitude(id){
      this.queryParams.typeId=id;
      this.queryParams.propertyLevel=1;
      this.loading = true;
      listProperty(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.properties = response.rows;
          this.total = response.total;
          this.loading = false;
          this.dialogTableVisible=true;
        }
      );
    },

    handleShowProperty(id){
      this.queryParams.typeId=id;
      this.queryParams.propertyLevel=2;
      this.loading = true;
      this.getProperList();
      this.dialogTableVisible=true;
    },

    getProperList(){
      listProperty(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.properties = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listType(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelProperForm() {
      this.properForm = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handlePropertyDelete(id){
      const propertyId = id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + propertyId + '"的数据项？').then(function() {
        return delProperty(propertyId);
      }).then(() => {
        this.getProperList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    handlePropertyAdd(){
      this.editForm={};
      this.properForm = true;
      this.properTitle = "添加属性";
      allType().then(response => {
        this.typeLists = response.data;
      });
    },

    handlePropertyEdit(id){
      allType().then(response => {
        this.typeLists = response.data;
      });
      getProperty(id).then(response => {
        this.editForm = response.data;
        this.properForm = true;
        this.properTitle = "修改属性";
      });
    },

    /** 提交按钮 */
    submitProperForm: function() {
      if (this.editForm.id != undefined) {
        updateProperty(this.editForm).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.properForm = false;
          this.getProperList();
        });
      } else {
        addProperty(this.editForm).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.properForm = false;
          this.getProperList();
        });
      }
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const typeId = row.id || this.ids;
      getType(typeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeId = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + typeId + '"的数据项？').then(function() {
          return delType(typeId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
  }
};
</script>
